package com.gxa.modules.service.zls.impl;

import com.gxa.common.vo.R2;
import com.gxa.modules.mapper.zls.LoginMapper;
import com.gxa.modules.service.zls.LoginService;
import com.gxa.modules.utils.DateUtils;
import com.gxa.modules.utils.MD5Utils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: zls
 * Date: 2021/12/28 12:01:02
 * Description:
 * Version: V1.0
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired(required = false)
    private RedisTemplate redisTemplate;
    @Autowired(required = false)
    private LoginMapper loginMapper;

    /**
     * 缓存注册短信验证码
     * @param keyCode
     * @param mobile_code
     */
    @Override
    public void saveMsgCode(String keyCode, int mobile_code) {
        //存入
        redisTemplate.boundValueOps(keyCode).set(mobile_code);
        //设置存在时间5分钟
        redisTemplate.boundValueOps(keyCode).expire(300, TimeUnit.MINUTES);
    }

    /**
     * 注册添加用户
     * @param map
     * @return
     */
    @Override
    public R2 addOrRegister(Map map) {
        Object check = map.get("jiaose");
        if(true == (Boolean) check){//注册消费者
            //获取手机号
            String user_phone = (String) map.get("user_phone");
            Map map1 = loginMapper.queryCustomer(user_phone);
            if(map1 != null){
                return new R2(500,"已注册过");
            }
            //获取输入的验证码
            Integer check_code = (Integer) map.get("check_code");
            String keyCode = user_phone + "login";
            Object c = redisTemplate.boundValueOps(keyCode).get();
            if(!check_code.equals(c)){
                return new R2(500,"验证码有误");
            }
            //获取用户名 密码
            String user_account = (String) map.get("user_account");
            String user_password = (String) map.get("user_password");
            //加密 加盐 散列
            String user_salt = "GXA";//盐值
            //新密码
            String newPwd = getNewPwd(user_password);
            int result = loginMapper.addCustomer2(user_account,newPwd,user_phone);
            if(result > 0) {
                return new R2(200,"注册成功");
            }
            return new R2(500,"注册失败");
        }else{//注册用户
            //获取手机号
            String user_phone = (String) map.get("user_phone");
            Map map1 = loginMapper.queryUser(user_phone);
            if(map1 != null){
                return new R2(500,"已注册过");
            }
            //获取输入的验证码
            Integer check_code = (Integer) map.get("check_code");
            String keyCode = user_phone + "login";
            Object c = redisTemplate.boundValueOps(keyCode).get();
            if(!check_code.equals(c)){
                return new R2(500,"验证码有误");
            }
            //获取用户名 密码
            String user_account = (String) map.get("user_account");
            String user_password = (String) map.get("user_password");
            //加密 加盐 散列
            String user_salt = "GXA";//盐值
            //新密码
            String newPwd = getNewPwd(user_password);
            //获取注册时间
            String user_date = DateUtils.getCurrentTime();
            int result = loginMapper.addUser2(user_account,newPwd,user_salt,user_phone,user_date);
            if(result > 0) {
                return new R2(200,"注册成功");
            }
            return new R2(500,"注册失败");
        }
    }

    /**
     * 修改密码
     * @param map
     * @return
     */
    @Override
    public R2 updatePassword(Map map) {
        //获取用户手机号
        String user_phone = (String) map.get("user_phone");
        //获取旧密码
        String user_password = (String) map.get("user_password");
        //获取新密码
        String new_password = (String) map.get("new_password");
        String newPwd = getNewPwd(user_password);
        //查询数据库
        List<Map> result = loginMapper.findUser2(user_phone,newPwd);
        if (result.size() == 1){
            //修改数据库密码
            String newPwd1 = getNewPwd(new_password);
            int r = loginMapper.updatePwd2(newPwd1,user_phone);
            if (r > 0){
                return new R2(200,"密码修改成功");
            }
        }
        return new R2(500,"密码修改失败");
    }

    /**
     * 缓存忘记密码短信验证码
     * @param keyCode
     * @param mobile_code
     */
    @Override
    public void saveMsgCode2(String keyCode, int mobile_code) {
        //存入
        redisTemplate.boundValueOps(keyCode).set(mobile_code);
        //设置存在时间分钟
        redisTemplate.boundValueOps(keyCode).expire(30, TimeUnit.MINUTES);
    }

    /**
     * 找回密码
     * @param map
     * @return
     */
    @Override
    public R2 updatePasswordForget(Map map) {
        //获取手机号
        String user_phone = (String) map.get("user_phone");
        //获取输入的验证码
        Integer check_code = (Integer) map.get("check_code");
        String keyCode = user_phone + "forget";
        Object c = redisTemplate.boundValueOps(keyCode).get();
//        if(!check_code.equals(c)){
//            return new R2(500,"验证码有误");
//        }
        //获取新密码
        String new_password = (String) map.get("new_password");
        String newPwd = getNewPwd(new_password);
        int i = loginMapper.updatePwd2(newPwd,user_phone);
        if (i > 0){
            return new R2(200,"密码已找回");
        }
        return new R2(500,"密码未找回");
    }

    /**
     * 消费者登录
     * @param map
     */
    @Override
    public R2 customerLogin(Map map) {
        Object user_phone = map.get("user_phone");
        Object user_password = map.get("user_password");
        Map map1 = loginMapper.customerLogin(user_phone,getNewPwd((String) user_password));
        if (map1 != null){
            return new R2(200,"登录成功",map1.get("customers_name"));
        }
        return new R2(500,"登录失败");
    }

    @Override
    public List<Map> queryShopInfo(String user_phone) {
        return loginMapper.queryShopInfo(user_phone);
    }

    @Override
    public List<Map> queryMenu() {
        return loginMapper.queryMenu();
    }

    @Override
    public Map queryRole1(String user_phone) {
        return loginMapper.queryRole1(user_phone);
    }

    /**
     * 加密 加盐 散列  新密码
     * @param user_password
     * @return
     */
    protected String getNewPwd(String user_password){
        //加密 加盐 散列
        String user_salt = "GXA";//盐值
        SimpleHash simpleHash = new SimpleHash(MD5Utils.MD5_KEY,user_password,user_salt,2);
//        Md5Hash md5Hash = new Md5Hash(user_password, user_salt, 2);
        //新密码
        String newPwd = simpleHash.toString();
        return newPwd;
    }

}
